<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>保险产品管理</el-breadcrumb-item>
      <el-breadcrumb-item>保险产品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <h3>保险产品列表</h3>
      <!-- 用户列表区域 -->
      <el-table :data="allInstances.data" border stripe>
        <el-table-column label="保险名称" prop="name"></el-table-column>
        <el-table-column label="投保金额" prop="price"></el-table-column>
        <el-table-column label="赔付金额" prop="payment"></el-table-column>
        <el-table-column label="有效时间" prop="duration"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="$router.push('/modifyaprofuct'+scope.row._id)">修改</el-button>
            <el-button size="mini" type="danger" @click="dele(scope.row._id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="allInstances.currentPage-0"
        :page-sizes="[3, 5, 8, 10]"
        :page-size="allInstances.pageSize-0"
        layout="total, sizes, prev, pager, next, jumper"
        :total="allInstances.total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapActions, mapState, mapMutations } = createNamespacedHelpers(
  "insurances"
);
export default {
  computed: {
    ...mapState(["allInstances"]),
  },
  created() {
    this.getInsurance();
  },
  methods: {
    ...mapActions(["getInsurance", "deleteInsurance"]),
    ...mapMutations(["setCurrent", "setPageSize"]),

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.setPageSize(val);
      this.getInsurance();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.setCurrent(val);
      this.getInsurance();
    },
    dele(row) {
      // 删除提示框
      this.$confirm("此操作将永久删除该客户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "danger",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.deleteInsurance(row);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
</style>